package com.firexxx.study;

import com.firexxx.study.asyncservlet.scene.TestScene;
import com.firexxx.study.performancetest.scene.MultiResult;
import com.firexxx.study.util.chart.ChartUtil;
import com.firexxx.study.web.RestTemplateHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.context.event.EventListener;

/**
 * TODO
 *
 * @author fireXXX
 * @date 2020/11/26 0:41
 **/
@SpringBootApplication(scanBasePackages = "com.firexxx.study")
@PropertySources({
        @PropertySource(value = "server.properties", ignoreResourceNotFound = true)
})
public class Server {

    @Autowired
    private TestScene testScene;

    @Autowired
    private RestTemplateHandler restTemplateHandler;

    private void start(int thread, String syncType, String taskType, int time) throws InterruptedException {
//        GridResult sceneResult = testScene.start(thread, syncType, taskType, time, 100);
//        ChartUtil.show(syncType + taskType + "任务" + thread + "线程" + time + "延时", sceneResult);

        MultiResult sceneResult = testScene.start(thread, syncType, taskType, time, 100);
        ChartUtil.show(syncType + taskType + "任务" + thread + "线程" + time + "延时", sceneResult);

//        SingleResult sceneResult = testScene.start1(thread, syncType, taskType, time, 100);
//        ChartUtil.show(syncType + taskType + "任务" + thread + "线程" + time + "延时", sceneResult);
    }

    @EventListener(ApplicationReadyEvent.class)
    public void testAsync() throws InterruptedException {

//        start(10, "sync", "io", 20);
//        start(10, "async", "io", 20);
//        start(20, "sync", "io", 20);
//        start(20, "async", "io", 20);
        start(5, "sync", "cpu", 100);
        start(10, "sync", "cpu", 100);
        start(15, "sync", "cpu", 100);
        start(20, "sync", "cpu", 100);
//        start(10, "async", "io", 100);

        Thread.sleep(1000000000);
    }

    public static void main(String[] args) {
        SpringApplicationBuilder builder = new SpringApplicationBuilder(Server.class);
        builder.headless(false).run(args);
    }

}